package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "User CRUD", tags = "User管理接口")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取所有用户")
    @GetMapping("/list")
    public List<User> list() {
        System.out.println("list");
        return userService.listUsers();
    }

    @ApiOperation("根据ID获取用户")
    @GetMapping("/{id}")
    public User getUser(@PathVariable("id") Long id) {
        return userService.getUserById(id);
    }

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public String save(@RequestBody User user) {
        boolean result = userService.saveUser(user);
        return result ? "success" : "fail";
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public String update(@RequestBody User user) {
        boolean result = userService.updateUser(user);
        return result ? "success" : "fail";
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        boolean result = userService.deleteUser(id);
        return result ? "success" : "fail";
    }
}
